using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;

namespace Xid.Controls
{
    public partial class GermPlasmPoolsMgrCtrl : UserControl
    {
        private Story story;
        private BindingSource bs = new BindingSource();
        private BindingList<IndividualsPool> bl;

        [Browsable(false)]
        public Story Story
        {
            get { return story; }
            set 
            {
                if (this.story != null)
                {
                    story.Pools.ItemAdded -= new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemAdded);
                    story.Pools.ItemMoved -= new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemMoved);
                    story.Pools.ItemRemoved -= new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemRemoved);
                    bs.DataSource = null;
                }
                story = value;
                if (story != null)
                {
                    this.labelNoStory.Visible = false;
                    story.Pools.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemAdded);
                    story.Pools.ItemMoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemMoved);
                    story.Pools.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<IndividualsPool>>(Pools_ItemRemoved);
                    bl = new BindingList<IndividualsPool>(story.Pools);
                    bs.DataSource = bl;
                  //  dataGridView1.DataSource = bs;
                    kryptonDataGridView1.DataSource = bs;
                    kryptonDataGridView1.Columns[1].ReadOnly = true;
                 //   dataGridView1.Columns[1].ReadOnly = true;
                    OnIndividualPoolsCountChanged();
                }
                else
                {
                    this.labelNoStory.Visible = true;
                }

            }
        }

        void Pools_ItemRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<IndividualsPool> e)
        {
            bl.ResetBindings();
            OnIndividualPoolsCountChanged();
        }

        private void OnIndividualPoolsCountChanged()
        {
            if (IndividualsPoolCountChanged != null)
                IndividualsPoolCountChanged(this, new EventArgs());
        }

        void Pools_ItemMoved(object sender, Mpi.Common.Collections.ItemEventArgs<IndividualsPool> e)
        {
            bl.ResetBindings();
          
        }

        void Pools_ItemAdded(object sender, Mpi.Common.Collections.ItemEventArgs<IndividualsPool> e)
        {
            bl.ResetBindings();
            OnIndividualPoolsCountChanged();
        }


        public GermPlasmPoolsMgrCtrl()
        {
            InitializeComponent();
            buttonSpecUp.Click += new EventHandler(OnMoveUp);
            buttonSpecDown.Click += new EventHandler(OnMoveDown);
            buttonSpecDelete.Click += new EventHandler(OnDelete);
            buttonSpecEdit.Click += new EventHandler(buttonSpecEdit_Click);
            buttonSpecAdd.Click += new EventHandler(buttonSpecAdd_Click);
            
        }

        void buttonSpecAdd_Click(object sender, EventArgs e)
        {
           if (this.GermPlasmEdit != null)
                    this.GermPlasmEdit(this,new GermPlasmEditEventargs(null));
        }

        void buttonSpecEdit_Click(object sender, EventArgs e)
        {
            if (bs.Current != null)
            {
                if (this.GermPlasmEdit != null)
                    this.GermPlasmEdit(this, new GermPlasmEditEventargs(bs.Current as IndividualsPool));
            }
        }

        void OnDelete(object sender, EventArgs e)
        {
            if (this.story != null && story.Pools.Count > 0)
            {
                story.Pools.RemoveAt(bs.CurrencyManager.Position);
            }
           // bs.CurrencyManager.RemoveAt(bs.CurrencyManager.Position);
        }

        private void OnMoveUp(object sender, EventArgs e)
        {
      
            IndividualsPool ip = bs.Current as IndividualsPool;

            if (this.story != null)
            {
                this.story.Pools.MoveUp(ip);
                bs.CurrencyManager.Position = story.Pools.IndexOf(ip);
            }
            
        }

        private void OnMoveDown(object sender, EventArgs e)
        {
            IndividualsPool ip = bs.Current as IndividualsPool;
            if (this.story != null)
            {
                this.story.Pools.MoveDown(ip);
                bs.CurrencyManager.Position = story.Pools.IndexOf(ip);
            }
        }

        public event EventHandler<GermPlasmEditEventargs> GermPlasmEdit;

        public event EventHandler IndividualsPoolCountChanged;

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (bs.Current != null)
            {
                if (this.GermPlasmEdit != null)
                    this.GermPlasmEdit(this, new GermPlasmEditEventargs(bs.Current as IndividualsPool));
            }
        }

    }

    public class GermPlasmEditEventargs : EventArgs
    {
        private IndividualsPool iPool;
        public IndividualsPool IndividualsPool
        {
            get { return iPool; }
        }

        public GermPlasmEditEventargs(IndividualsPool ip)
        {
            this.iPool = ip;
        }
    }
}
